#include <QtSql>
#include <QtGui>

#include "gudaywellwindow.h"

GudaywellWindow::GudaywellWindow(QWidget *parent)
	:QMainWindow(parent)
{	
	setupUi(this);
	
	langAGroup = new QActionGroup(this);
	langAGroup->addAction(actionArabic);
	langAGroup->addAction(actionEnglish);
	langAGroup->setExclusive(true);
	
	db = new DbAdmin;
	setCentralWidget(&daysForm);
	makeSlotsConnections();
	newFile();
}

GudaywellWindow::~GudaywellWindow()
{
	closeFile();
}
//*****************************************************************//
void GudaywellWindow::makeSlotsConnections()
{
	connect(actionSetting,SIGNAL( triggered() ),this,SLOT(showSetting()));
	connect(actionNew,SIGNAL(triggered()),this,SLOT( newFile()));
	connect(actionClose,SIGNAL(triggered()),this,SLOT( closeFile()));
	connect(actionOpen,SIGNAL(triggered()),this,SLOT( open()));
	connect(actionSave,SIGNAL(triggered()),this,SLOT( save()));
	connect(actionSaveAs,SIGNAL(triggered()),this,SLOT( saveAs()));
	connect(actionAbout,SIGNAL(triggered()),this,SLOT( about()));
	connect(actionAboutQt,SIGNAL(triggered()),this,SLOT( aboutQt()));
	connect(actionArabic,SIGNAL(toggled(bool)),this,SLOT(changeLang(bool)));

}
//*******************************
//			S L O T S 
//*****************************************************************//
void GudaywellWindow::showSetting()
{
	setting.exec();
}
//*****************************************************************//
void GudaywellWindow::about()
{
    QMessageBox::about(this, tr("About Gudaywell"),
            tr("<h2>Gudaywell 0.0.2</h2>"
               "<p>Copyright &copy; 2006 Mohamed Assar mohasr@gmail.com"
               "<p>Gudaywell is a small application for "
               "managing school schedules."));
}
//*****************************************************************//
void GudaywellWindow::aboutQt()
{
	QMessageBox::aboutQt(this);
}
//*****************************************************************//
void GudaywellWindow::newFile()
{
	closeFile();

	QTemporaryFile file("tmp_gudXXXXXX");
	file.setAutoRemove(false);
	if (file.open()) {
		QFileInfo fiGud(file);
		currentTempFile = fiGud.absoluteFilePath();
		db->createConnection( currentTempFile ,true);
		setting.connectModel();
		file.close();
	}
	currentFile = tr("new file");
	updateTitle();
	daysForm.refresh();
	setting.refresh();
}
//*****************************************************************//
void GudaywellWindow::open()
{
	const QString file = QFileDialog::getOpenFileName(this,
                                   tr("Open Schedule"), ".",
                                   tr("Gudaywell files (*.gdw)"));
                                   
	if ( QFile::exists(file) )
	{
		closeFile();
		QFile::copy(file,currentTempFile);
		QFile tempFile(currentTempFile);
		if (tempFile.open(QIODevice::ReadWrite)) {
			QFileInfo fiGud(tempFile);
			db->createConnection( currentTempFile , false );
			setting.connectModel();
			tempFile.close();
		}
		currentFile = file;
		daysForm.refresh();
		setting.refresh();
		updateTitle();
	}
}
//*****************************************************************//
void GudaywellWindow::save(QString file)
{
	setting.submit();
	if ( QFile::exists(file) ) QFile::remove(file);
	QFile::copy(currentTempFile, file);
	currentFile = file;
    updateTitle();
}
//*****************************************************************//
void GudaywellWindow::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                               tr("Save Schedule"), ".",
                               tr("Gudaywell files (*.gdw)"));

    if (!fileName.isEmpty()){
    	if(!fileName.endsWith(".gdw",Qt::CaseInsensitive) )
    		fileName.append(".gdw");
    		
    	save(fileName);
	} 
}
//*****************************************************************//
void GudaywellWindow::saveFile()
{
	if ( QFile::exists(currentFile) ) save(currentFile);
	else saveAs();
}

//*****************************************************************//
void GudaywellWindow::closeFile()
{
	db->closeConnection();
	if ( QFile::exists(currentTempFile) ) 
		QFile::remove( currentTempFile );
}

void GudaywellWindow::changeLang(bool arabicLang)
{
	if ( arabicLang ){
    	;//set arabic as language
	}else{
		;//set english as language
	}
}
//***********************************
//		M E T H O D S
//*****************************************************************//
QString GudaywellWindow::strippedName(const QString &fullFileName)
{
    return QFileInfo(fullFileName).fileName();
}
//*****************************************************************//
bool GudaywellWindow::okToContinue()
{
    if (isWindowModified()) {
        int r = QMessageBox::warning(this, tr("Gudaywell"),
                        tr("The document has been modified.\n"
                           "Do you want to save your changes?"),
                        QMessageBox::Yes | QMessageBox::Default,
                        QMessageBox::No,
                        QMessageBox::Cancel | QMessageBox::Escape);
        if (r == QMessageBox::Yes) {
            saveFile();
        } else if (r == QMessageBox::Cancel) {
            return false;
        }
    }
    return true;
}
//*****************************************************************//
void GudaywellWindow::closeEvent(QCloseEvent *event)
{
    if (okToContinue()) {
        event->accept();
    } else {
        event->ignore();
    }
}
//*****************************************************************//
void GudaywellWindow::updateTitle()
{
	this->setWindowTitle(tr("Gudaywell - ") + currentFile + "[*]");
}
//*****************************************************************//
void GudaywellWindow::modified()
{
    setWindowModified(true);
}
